﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TanmiaGrp.Basis.Core;
using TanmiaGrp.GeneralLedger.Core;
using TanmiaGrp.GeneralLedger.Domain;

namespace TanmiaGrp.GeneralLedger.WinApp
{
    public static class DomainExtensions
    {
        public static Task<int> GetTotalCountAsync<TBusinessObject, TBusinessDomain>(this BaseDomain<TBusinessObject, TBusinessDomain> domain)
            where TBusinessObject : BusinessObject, new()
            where TBusinessDomain : BaseDomain<TBusinessObject, TBusinessDomain>, new()
        {
            return Task.Factory.StartNew<int>(() => domain.GetTotalCount());
        }

        public static Task<List<TBusinessObject>> ReadAsync<TBusinessObject, TBusinessDomain>(this BaseDomain<TBusinessObject, TBusinessDomain> domain, int pageIndex)
            where TBusinessObject : BusinessObject, new()
            where TBusinessDomain : BaseDomain<TBusinessObject, TBusinessDomain>, new()
        {
            return Task.Factory.StartNew<List<TBusinessObject>>(() => domain.GetPage(pageIndex));
        }

        public static Task<bool> DeleteAsync<TBusinessObject, TBusinessDomain>(this BaseDomain<TBusinessObject, TBusinessDomain> domain, IEnumerable<TBusinessObject> entities)
            where TBusinessObject : BusinessObject, new()
            where TBusinessDomain : BaseDomain<TBusinessObject, TBusinessDomain>, new()
        {
            return Task.Factory.StartNew<bool>(() => domain.Delete(entities));
        }

        public static Task<List<TBusinessObject>> SearchPageAsync<TBusinessObject, TBusinessDomain>(this BaseDomain<TBusinessObject, TBusinessDomain> domain, SearchSpecification searchSpecs)
            where TBusinessObject : BusinessObject, new()
            where TBusinessDomain : BaseDomain<TBusinessObject, TBusinessDomain>, new()
        {
            return Task.Factory.StartNew<List<TBusinessObject>>(() => domain.SearchPage(searchSpecs));
        }
    }
}
